package com.jpcc.vw.onlineRadio.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jpcc.vw.onlineRadio.utils.PermissionReq;

/**
 * 基础Fragment
 * Created by liukanshan on 2016/12/8.
 */

public abstract class BaseFragment extends Fragment {
    public View mContextView;
    private Context mContext;
    private MaterialDialog progressDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContextView=inflater.inflate(getLayoutId(),container,false);
        initProgressDialog();
        setupView();
        setupData();
        mContext=getContext();
        return mContextView;
    }

    private void initProgressDialog(){
        progressDialog = new MaterialDialog.Builder(getActivity())
                .progress(true, 0)
                .content("请等待...")
                .autoDismiss(false)
                .cancelable(true)
                .build();
    }

    public void showProgress(){
        progressDialog.show();
    }

    public void hideProgress(){
        progressDialog.dismiss();
    }

    public abstract int getLayoutId();

    public abstract void setupView();

    public abstract void setupData();

    public <T extends View> T $(int id) {
        return (T) mContextView.findViewById(id);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionReq.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
